// pages/serviceEval/serviceEval.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    eval: [
      // {id:1,orderid:32489565655656,sumper:45,tel:14578651245,state:1,tag:1,address:'广州市番禺区石岗路地址地址地址地址地址',user:'刘德华',active:3,time:'2020/09/06'},
    ],
    value: 0,
    type: null,
    textArea: '', //输入信息
    submitbtn: true,
    markname: '待服务',
    select: [{
      name: '服务有礼',
      index: 0
    }, {
      name: '专业讲师',
      index: 0
    }, {
      name: '最佳指导',
      index: 0
    }],
    Model: false,
    hiddenmodalput: true,
    disabled: false,
  },
  //标签多选
  // checkbox: function (e) {
  //   var index = e.currentTarget.dataset.index;//获取当前点击的下标
  //   var checkboxArr = this.data.checkboxArr;//选项集合
  //   checkboxArr[index].checked = !checkboxArr[index].checked;//改变当前选中的checked值
  //   this.setData({
  //    checkboxArr: checkboxArr
  //   });
  //  },
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
  //点击弹出
  modalinput() {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  choose(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var select = this.data.select;
    select[index].index = (select[index].index!=1?1:0)
    // if (select[index].index != 1) {
    //   select[index].index = 1
    // } else {
    //   select[index].index = 0
    // }
    that.setData({
      select: select
    })
    console.log(select)
  },
  customize(e) {
    var that = this;
    that.setData({
      customize: e.detail.value
    })
  },
  showModel(e) {
    var that = this;
    that.setData({
      Model: true,
      customize: ''
    })
  },
  //取消按钮
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认
  confirm: function () {
    this.setData({
      hiddenmodalput: true
    })
  },
  //文字评论
  bindTextAreaBlur: function (e) {
    this.setData({
      textArea: e.detail.value
    })
  },
  submit(e) {
    let that = this;
    var i = e.currentTarget.dataset.status; //订单状态
    var type = that.data.type;
    let rate = that.data.value;
    let texteare = that.data.textArea;
    let json = {
      pk: that.data.eval.id
    }
    let data = {
      rate: rate,
      comment: texteare
    }
    // console.log(i)
    if (i == 2 || i == 3) {
      if (!data.rate || !data.comment) {
        wx.showToast({
          title: '你没有填写评价哦',
          icon: 'none',
          duration: 1500
        })
        return false //阻止发起请求
      }
      switch (type) {
        case 1: //优选
          app.ports.api60({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
        case 2: //折扣
          app.ports.api61({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
        case 3: //放盘
          app.ports.api62({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
        case 4: //尽调
          app.ports.api63({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
        case 5: //好房
          app.ports.api64({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
        case 6: //增值
          app.ports.api65({
            json,
            data
          }).then((res) => {
            // console.log(res)
            wx.showToast({
              title: '评价成功',
              icon: 'success',
              duration: 1500
            })
          }).catch(() => {
            console.error('评论错误！')
          });
          break
      }
      that.onLoad(that.data.id)
    } else {
      wx.showToast({
        title: '订单还没成功！无法评价',
        icon: 'none',
        duration: 1500
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // console.log(options)
    let type = Number(options.type);
    this.setData({
      type: type,
      id: options.id
    })
    let data = {
      pk: options.id
    }
    switch (type) {
      case 1: //优选预约
        getApp().ports.api2({
          data: data
        }).then((res) => {
          // console.log(res)
          let list = res.data;
          if (list.rate && list.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: list.rate
            }); //获取评论数据
            that.setData({
              texteare: list.comment
            });
          }
          res.data.orderTime = options.time.substring(0, 10);
           if (res.data.stage != 3) that.setData({ disabled : true });
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            list.telphone = false
          } else {
            list.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            list.markname = '待服务';
          } else {
            list.markname = options.markName;
          }
          list.arr = options.time.substring(5, 16).split(" ");
          if (list.foreclosedHouse.startTime) list.arr1 = list.foreclosedHouse.startTime.substring(5, 16).split(" ");
          if (list.foreclosedHouse.endTime) list.arr2 = list.foreclosedHouse.endTime.substring(5, 16).split(" ");
          that.setData({
            eval: list
          })
        }).catch(() => {
          console.error('优选预约点击页面出错了？');
        });
        break;
      case 2: //折扣 
        getApp().ports.api6({
          data: data
        }).then((res) => {
          // console.log(res.data);
          let dataList = res.data;
          if (dataList.rate && dataList.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: dataList.rate
            }); //获取评论数据
            that.setData({
              texteare: dataList.comment
            });
          }
           if (res.data.stage != 3) that.setData({ disabled : true });
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            dataList.telphone = false
          } else {
            dataList.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            dataList.markname = '待服务';
          } else {
            dataList.markname = options.markName;
          }
          dataList.discountHouse.banner = that.data.imageRoot + dataList.discountHouse.banner;
          dataList.orderTime = options.time.substring(0, 10);
          dataList.arr = options.time.substring(5, 16).split("T");
          if (res.data.servingTime) dataList.arr1 = res.data.servingTime.split(" ");
          if (res.data.finishTime) dataList.arr2 = res.data.finishTime.split(" ");
          that.setData({
            eval: dataList
          })
        }).catch(() => {
          console.error('出啥错了？');
        });
        break;
      case 3: //放盘
        getApp().ports.api9({
          data: data
        }).then((res) => {
          let datalist = res.data;
          console.log(datalist);
          datalist.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
          if (datalist.rate && datalist.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: datalist.rate
            }); //获取评论数据
            that.setData({
              texteare: datalist.comment
            });
          }
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            datalist.telphone = false
          } else {
            datalist.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            datalist.markname = '待服务';
          } else {
            datalist.markname = options.markName;
          }
           if (res.data.stage != 3) that.setData({ disabled : true });
          datalist.arr = options.time.substring(5, 16).split(" ");
          datalist.orderTime = options.time.substring(0, 10);
          if (datalist.servingTime) datalist.arr1 = datalist.servingTime.substring(5, 16).split(" ");
          if (datalist.finishTime) datalist.arr2 = datalist.finishTime.substring(5, 16).split(" ");
          that.setData({
            eval: datalist
          })
        }).catch(() => {
          console.error('放盘错了？')
        });
        break;
      case 4: //尽调 
        getApp().ports.api16({
          data: data
        }).then((res) => {
          let datalist = res.data;
          //       console.log(datalist);
          if (datalist.rate && datalist.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: datalist.rate
            }); //获取评论数据
            that.setData({
              texteare: datalist.comment
            });
          }
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            datalist.telphone = false
          } else {
            datalist.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            datalist.markname = '待服务';
          } else {
            datalist.markname = options.markName;
          }
           if (res.data.stage != 3) that.setData({ disabled : true });
          datalist.arr = options.time.substring(5, 16).split(" ");
          datalist.orderTime = options.time.substring(0, 10);
          if (datalist.foreclosedHouse.startTime) datalist.arr1 = datalist.foreclosedHouse.startTime.substring(5, 16).split(" ");
          if (datalist.foreclosedHouse.endTime) datalist.arr2 = datalist.foreclosedHouse.endTime.substring(5, 16).split(" ");
          that.setData({
            eval: datalist
          })
        }).catch(() => {
          console.error('尽调错了？')
        });
        break;
      case 5: //好房 
        getApp().ports.api122({
          data: data
        }).then((res) => {
          let datalist = res.data;
          datalist.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
          //       console.log(datalist);
          if (datalist.rate && datalist.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: datalist.rate
            }); //获取评论数据
            that.setData({
              texteare: datalist.comment
            });
          }
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            datalist.telphone = false
          } else {
            datalist.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            datalist.markname = '待服务';
          } else {
            datalist.markname = options.markName;
          }
           if (res.data.stage != 3) that.setData({ disabled : true });
          datalist.zdyTitle = '好房预约';
          datalist.arr = options.time.substring(5, 16).split(" ");
          datalist.orderTime = options.time.substring(0, 10);
          if (res.data.servingTime) res.data.arr1 = res.data.servingTime.split(" ");
          if (res.data.finishTime) res.data.arr2 = res.data.finishTime.split(" ");
          that.setData({
            eval: datalist
          })
        }).catch(() => {
          console.error('好房错了？')
        });
        break;
      case 6: //增值 
        getApp().ports.api144({
          data: data
        }).then((res) => {
          let datalist = res.data;
          datalist.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
          //       console.log(datalist);
          if (datalist.rate && datalist.comment) {
            that.setData({
              submitbtn: false
            })
            that.setData({
              value: datalist.rate
            }); //获取评论数据
            that.setData({
              texteare: datalist.comment
            });
          }
           if (res.data.stage != 3) that.setData({ disabled : true });
          if (options.telphone == 'null' || options.telphone == 'undefined') {
            datalist.telphone = false
          } else {
            datalist.telphone = options.telphone
          }
          if (options.markName == 'null' || options.markName == 'undefined' || options.markName == '') {
            datalist.markname = '待服务';
          } else {
            datalist.markname = options.markName;
          }
          datalist.zdyTitle = '增值预约';
          datalist.arr = options.time.substring(5, 16).split(" ");
          datalist.orderTime = options.time.substring(0, 10);
          if (datalist.servingTime) datalist.arr1 = datalist.servingTime.substring(5, 16).split(" ");
          if (datalist.finishTime) datalist.arr2 = datalist.finishTime.substring(5, 16).split(" ");
          that.setData({
            eval: datalist
          })
        }).catch(() => {
          console.error('增值错了？')
        });
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})